#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n;

int main() {
    cin >> n;
    int p[n];
    int w[n];
    for (int i = 0; i < n; i++) cin >> p[i];
    for (int i = 0; i < n; i++) cin >> w[i];
    int maxRoom = *max_element(p, p + n);
    vector<__int128_t> dp(maxRoom + 1, 0);
    for (int i = 0; i < n; i++) dp[p[i]] += w[i];
    for (int i = maxRoom; i >= 1; i--) {
        __int128_t add = 0;
        if (2 * i <= maxRoom) add = max(add, dp[2 * i]);
        if (2 * i + 1 <= maxRoom) add = max(add, dp[2 * i + 1]);
        dp[i] += add;
    }
    cout << (long long)dp[1] << endl;
    return 0;
}